//
//  LoginController.m
//  UESTCPioneer
//
//  Created by Apple on 14-11-13.
//  Copyright (c) 2014年 Sway. All rights reserved.
//

#import "LoginController.h"
#import "registViewController.h"
#import "FindPwdViewController.h"
#import "AppDelegate.h"
#import "MBProgressHUD.h"
@interface inputRect : UIView
@property (nonatomic)UILabel *userNameLabel,*passwordLabel;
@property (nonatomic)UITextField *userNameField,*passwordField;

@end

@implementation inputRect

-(id)init{
    self = [super init];
    if (self){
        //背景
        UIImageView *backgroundImageView = [[UIImageView alloc]init];
        //        [backgroundImageView setImage:[UIImage imageNamed:@"inputkuang"]];
        [self addSubview:backgroundImageView];
        [backgroundImageView.layer setCornerRadius:5];
        [backgroundImageView.layer setBorderWidth:1];
        [backgroundImageView.layer setBorderColor:[UIColor colorWithWhite:146.0/255.0 alpha:0.5].CGColor];
        //        [backgroundImageView setContentMode:UIViewContentModeRedraw];
        [backgroundImageView setTranslatesAutoresizingMaskIntoConstraints:NO];
        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[backgroundImageView]-10-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(backgroundImageView)]];
        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-8-[backgroundImageView]-8-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(backgroundImageView)]];
        
        
        //分割线
        UIImageView *seperator = [[UIImageView alloc]init];
        [seperator setImage:[UIImage imageNamed:@"sepline"]];
        [self addSubview:seperator];
        [seperator setContentMode:UIViewContentModeRedraw];
        [seperator setTranslatesAutoresizingMaskIntoConstraints:NO];
        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[seperator]-10-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(seperator)]];
        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[seperator(==0.5)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(seperator)]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:seperator attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:seperator attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
        
        //帐号标题
        _userNameLabel=[[UILabel alloc]init];
        _userNameLabel.text=@"帐号：";
        [self addSubview:_userNameLabel];
        [_userNameLabel setAdjustsFontSizeToFitWidth:YES];
        [_userNameLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:_userNameLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:backgroundImageView attribute:NSLayoutAttributeHeight multiplier:0.5 constant:0]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:_userNameLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:backgroundImageView attribute:NSLayoutAttributeLeft multiplier:1 constant:10]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:_userNameLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:backgroundImageView attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:_userNameLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:56]];
        
        _passwordLabel=[[UILabel alloc]init];
        _passwordLabel.text=@"密码：";
        [self addSubview:_passwordLabel];
        [_passwordLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:_passwordLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:backgroundImageView attribute:NSLayoutAttributeHeight multiplier:0.5 constant:0]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:_passwordLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:backgroundImageView attribute:NSLayoutAttributeLeft multiplier:1 constant:10]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:_passwordLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:backgroundImageView attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:_passwordLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:56]];
        
        
        _userNameField=[[UITextField alloc]init];
        [self addSubview:_userNameField];
        [_userNameField setKeyboardType:UIKeyboardTypeEmailAddress];
        [_userNameField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
        //        textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
        [_userNameField setPlaceholder:@"请输入用户名"];
        [_userNameField setTranslatesAutoresizingMaskIntoConstraints:NO];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:_userNameField attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:_userNameLabel attribute:NSLayoutAttributeRight multiplier:1 constant:0]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:_userNameField attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:backgroundImageView attribute:NSLayoutAttributeHeight multiplier:0.5 constant:0]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:_userNameField attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:backgroundImageView attribute:NSLayoutAttributeRight multiplier:1 constant:0]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:_userNameField attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_userNameLabel attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
        
        //密码输入框
        _passwordField=[[UITextField alloc]init];
        [self addSubview:_passwordField];
        [_passwordField setPlaceholder:@"请输入密码"];
        [_passwordField setTranslatesAutoresizingMaskIntoConstraints:NO];
        [_passwordField setSecureTextEntry:YES];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:_passwordField attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:_passwordLabel attribute:NSLayoutAttributeRight multiplier:1 constant:0]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:_passwordField attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:backgroundImageView attribute:NSLayoutAttributeHeight multiplier:0.5 constant:0]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:_passwordField attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:backgroundImageView attribute:NSLayoutAttributeRight multiplier:1 constant:0]];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:_passwordField attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_passwordLabel attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
        
        
        if ([[NSUserDefaults standardUserDefaults]objectForKey:kMY_USER_LoginName]) {
            [_userNameField setText:[[NSUserDefaults standardUserDefaults]objectForKey:kMY_USER_LoginName]];
        }
        if ([[NSUserDefaults standardUserDefaults]objectForKey:kMY_USER_PASSWORD]) {
            [_passwordField setText:[[NSUserDefaults standardUserDefaults]objectForKey:kMY_USER_PASSWORD]];
        }
    }
    return self;
}

@end

@interface LoginController ()<IChatManagerDelegate,UIAlertViewDelegate,UITextFieldDelegate>{
    UILabel *welcomeLabel;
    UITextField *userNameField,*passwordField;
    inputRect *inputRectV;
    UIButton *registButton,*loginButton;
    UIButton * findPwd;
//    UIActivityIndicatorView *_activityView;
}

@property (nonatomic,strong) UIView * indicatorView;
@end

@implementation LoginController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title=@"登录帐号";
    
    //标题
    welcomeLabel=[[UILabel alloc]init];
    [welcomeLabel setTextAlignment:NSTextAlignmentCenter];
    [welcomeLabel setText:@"欢迎登录成电先锋"];
    [self.view addSubview:welcomeLabel];
    [welcomeLabel setTextColor:kNavigationBarColor];
    
    //输入框
    inputRectV = [[inputRect alloc]init];
    [self.view addSubview:inputRectV];
    [inputRectV setTranslatesAutoresizingMaskIntoConstraints:NO];
    
    [welcomeLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-8-[welcomeLabel(==44)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(welcomeLabel)]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[welcomeLabel]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(welcomeLabel)]];
    
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[welcomeLabel][inputRectV(==88)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(inputRectV,welcomeLabel)]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[inputRectV]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(inputRectV)]];
    
    userNameField=inputRectV.userNameField;
    passwordField=inputRectV.passwordField;
    
    //注册按钮
    registButton = [[UIButton alloc]init];
    [registButton setTitle:@"注册" forState:UIControlStateNormal];
    [registButton.layer setCornerRadius:5];
    [registButton.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [registButton setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1]];
    [registButton setShowsTouchWhenHighlighted:YES];
    [registButton setTranslatesAutoresizingMaskIntoConstraints:NO];
    [registButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view addSubview:registButton];
    [registButton setTranslatesAutoresizingMaskIntoConstraints:NO];
    //    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:registButton attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:inputRectView attribute:NSLayoutAttributeLeft multiplier:1 constant:0]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-16-[registButton(==100)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(registButton)]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[inputRectV]-30-[registButton(==34)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(registButton,inputRectV)]];
    
    
    [registButton addTarget:self action:@selector(regist:) forControlEvents:UIControlEventTouchUpInside];
    
    //登录按钮
    loginButton = [[UIButton alloc]init];
    [loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [loginButton.layer setCornerRadius:5];
    [loginButton.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [loginButton setBackgroundColor:kNavigationBarColor];
    [loginButton setShowsTouchWhenHighlighted:YES];
    [loginButton setTranslatesAutoresizingMaskIntoConstraints:NO];
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    
    [loginButton addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:loginButton];
    [loginButton setTranslatesAutoresizingMaskIntoConstraints:NO];
    //    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:registButton attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:inputRectView attribute:NSLayoutAttributeLeft multiplier:1 constant:0]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[registButton]-16-[loginButton]-16-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(loginButton,registButton)]];
    //    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[inputRectView]-20-[registButton(==34)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(registButton,inputRectView)]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:loginButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:registButton attribute:NSLayoutAttributeHeight multiplier:1 constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:loginButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:registButton attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
    
    
    findPwd = [[UIButton alloc]init];
    [findPwd setTitle:@"找回密码?" forState:UIControlStateNormal];
    [findPwd setTitle:@"找回密码?" forState:UIControlStateHighlighted];
    [findPwd setTitle:@"找回密码?" forState:UIControlStateSelected];
    //    [findPwd.layer setCornerRadius:5];
    [findPwd.titleLabel setFont:[UIFont systemFontOfSize:15]];
    //    [findPwd setBackgroundColor:kNavigationBarColor];
    findPwd.titleLabel.textColor = [UIColor blueColor];
    [findPwd setShowsTouchWhenHighlighted:YES];
    [findPwd setTranslatesAutoresizingMaskIntoConstraints:NO];
    [findPwd setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    
    
    [findPwd addTarget:self action:@selector(findPassword:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:findPwd];
    [findPwd setTranslatesAutoresizingMaskIntoConstraints:NO];
    //    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:registButton attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:inputRectView attribute:NSLayoutAttributeLeft multiplier:1 constant:0]];
    //    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[registButton]-16-[loginButton]-16-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(loginButton,registButton)]];
    //    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[inputRectView]-20-[registButton(==34)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(registButton,inputRectView)]];
    //    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:findPwd attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:registButton attribute:NSLayoutAttributeHeight multiplier:1 constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:findPwd attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:inputRectV attribute:NSLayoutAttributeBottom multiplier:1 constant:5]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:findPwd attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:inputRectV attribute:NSLayoutAttributeLeft multiplier:1 constant:12]];
    //    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:findPwd attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:loginButton attribute:NSLayoutAttributeRight multiplier:1 constant:0]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[findPwd(==20)]" options:0 metrics:Nil views:NSDictionaryOfVariableBindings(findPwd)]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[findPwd(==80)]" options:0 metrics:Nil views:NSDictionaryOfVariableBindings(findPwd)]];
    
    //	// Do any additional setup after loading the view.
//    CGFloat x = (self.view.frame.size.width - 37)/2;
//    CGFloat y = (self.view.frame.size.height - 37)/2;
//    self.indicatorView = [[UIView alloc] initWithFrame:CGRectMake(x, y - 90, 38, 38)];
//    [self.view addSubview:self.indicatorView];
//    //设置对象的位置，大小是固定不变的。WhiteLarge为37 * 37，White为20 * 20
//    _activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:
//                     UIActivityIndicatorViewStyleWhiteLarge] ;
//    
//	[self.indicatorView addSubview:_activityView];
//    //    _activityView.hidesWhenStopped = NO;
//    //    [self.indicatorView setBackgroundColor:[UIColor grayColor]];
//    _activityView.color = [UIColor grayColor];
    
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [inputRectV.userNameField resignFirstResponder];
    [inputRectV.passwordField resignFirstResponder];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - registButton
-(void)regist:(UIButton*)button{
    
    NSArray *mut = @[@"昵       称:",@"邮       箱:",@"身份证号:",@"所属党委:",@"所属单位:",@"用户职位:",@"选择支部:",@"输入密码:",@"确认密码:"];
    
    registViewController *registController = [[registViewController alloc]initWithLabelTextArray:mut];
    [self.navigationController pushViewController:registController animated:YES];
    
}

#pragma mark - loginButton
-(void)login:(UIButton *)button{
    [self.view endEditing:YES];
    //显示指示器
    MBProgressHUD * hud  = [MBProgressHUD showHUDAddedTo:self.view.window animated:YES];
    hud.labelText = @"登录中...";
    hud.dimBackground = YES;
    
    NSDictionary *requestData=@{@"type":@"login"
                                ,@"userName":userNameField.text
                                ,@"passwd":passwordField.text};
    
    [NetworkCenter AFRequestWithData:requestData SuccessBlock:^(AFHTTPRequestOperation *operation, id resultObject) {
        NSArray *arr = [NSJSONSerialization JSONObjectWithData:resultObject options:NSJSONReadingMutableLeaves error:nil];
        NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
        [dic addEntriesFromDictionary:arr[0]];
        NSLog(@"dic = %@",dic);
        if ([dic[@"result"] isEqualToString:@"success"]){
            [constant setPersonalInfo:dic[@"info"]];
            [constant setName:dic[@"info"][@"name"]];
            [constant setUserId:dic[@"info"][@"userID"]];
            [constant setUserName:dic[@"info"][@"userName"]];
            [constant setUserInstitute:dic[@"info"][@"institute"]];
            
            NSLog(@"[constant getUserInstitute] = %@",[constant getUserInstitute]);
            NSUserDefaults * defaultData = [NSUserDefaults standardUserDefaults];
            [defaultData setObject:dic[@"info"] forKey:@"personalInfo"];
            
            [[NSUserDefaults standardUserDefaults] setObject:userNameField.text forKey:kMY_USER_LoginName];
            [[NSUserDefaults standardUserDefaults] setObject:passwordField.text forKey:kMY_USER_PASSWORD];
            
            //清除指示器
            [MBProgressHUD hideAllHUDsForView:self.view.window animated:YES];
            [defaultData setBool:YES forKey:@"login"];
            [defaultData synchronize];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"login" object:nil];
            
            if(![constant checkAccount]){
                //环信登录
                [self doLogin];
            }
        } else {
            //清除指示器
            [MBProgressHUD hideAllHUDsForView:self.view.window animated:YES];
            [Alert showAlert:@"登录失败"];
        }
    } FailureBlock:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"operation.responseString = %@",operation.responseString);
        [Alert showAlert:@"网络连接出错"];
        NSLog(@"网络连接出错 = %@",error.localizedDescription);
        //清除指示器
        [MBProgressHUD hideAllHUDsForView:self.view.window animated:YES];
    }];
}

/*********************************************环信****************************************************/
- (void)loginWithUsername:(NSString *)username password:(NSString *)password
{
    [self showHudInView:self.view hint:@"正在登录..."];
    [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:username
                                                        password:password
                                                      completion:
     ^(NSDictionary *loginInfo, EMError *error) {
         NSLog(@"error.description = %@,error.errorCode = %i",error.description,error.errorCode);
         [self hideHud];
         if (loginInfo && !error) {
             [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@YES];
             EMPushNotificationOptions *options = [[EaseMob sharedInstance].chatManager pushNotificationOptions];
             NSLog(@"options = %@",options);
             NSLog(@"");
         }else {
             switch (error.errorCode) {
                 case EMErrorServerNotReachable:
//                     TTAlertNoTitle(@"连接服务器失败!");
                     break;
                 case EMErrorServerAuthenticationFailure:
//                     TTAlertNoTitle(@"用户名或密码错误");
                     break;
                 case EMErrorServerTimeout:
//                     TTAlertNoTitle(@"连接服务器超时!");
                     break;
                 default:
//                     TTAlertNoTitle(@"登录失败");
                     break;
             }
         }
     } onQueue:nil];
}

- (void)doLogin {
    if (![self isEmpty]) {
        [self.view endEditing:YES];
        if ([userNameField.text isChinese]) {
            UIAlertView *alert = [[UIAlertView alloc]
                                  initWithTitle:@"用户名不支持中文"
                                  message:nil
                                  delegate:nil
                                  cancelButtonTitle:@"确定"
                                  otherButtonTitles:nil];
            
            [alert show];
            
            return;
        }
//#if !TARGET_IPHONE_SIMULATOR
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
//                                                        message:@"填写推送消息时使用的昵称"
//                                                        delegate:self
//                                                        cancelButtonTitle:@"取消"
//                                                        otherButtonTitles:@"确定", nil];
//        [alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
//        UITextField *nameTextField = [alert textFieldAtIndex:0];
//        nameTextField.text = [constant getName];
//        [alert show];
//        NSLog(@"真机提示框显示");
//#elif TARGET_IPHONE_SIMULATOR
        [self loginWithUsername:[constant getUserId] password:passwordField.text];
//#endif
    }
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if ([alertView cancelButtonIndex] != buttonIndex) {
        UITextField *nameTextField = [alertView textFieldAtIndex:0];
        if(nameTextField.text.length > 0)
        {
            [[EaseMob sharedInstance].chatManager setNickname:nameTextField.text];
        }
    }
    
    [self loginWithUsername:[constant getUserId] password:passwordField.text];
}

- (BOOL)isEmpty{
    BOOL ret = NO;
    NSString *username = userNameField.text;
    NSString *password = passwordField.text;
    if (username.length == 0 || password.length == 0) {
        ret = YES;
        [WCAlertView showAlertWithTitle:@"提示"
                                message:@"请输入账号和密码"
                     customizationBlock:nil
                        completionBlock:nil
                      cancelButtonTitle:@"确定"
                      otherButtonTitles: nil];
    }
    
    return ret;
}

/*************************************************************************************************/
#pragma mark 找回密码
-(void)findPassword:(UIButton *)btn{
    FindPwdViewController * controller = [[FindPwdViewController alloc] init];
    controller.navigationItem.title = @"找回密码";
    [self.navigationController pushViewController:controller animated:NO];
}

@end
